﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Manageweb_MemberMaster : System.Web.UI.MasterPage
{
    #region Property

    //用户名
    private string userName;
    public string UserName
    {
        get
        {

            return ViewState["userName"].ToString();
        }
        set
        {
            userName = value;
            ViewState["userName"] = value;

        }
    }
    //用户账号
    private string userAccount;
    public string UserAccount
    {
        get
        { return ViewState["userAccount"].ToString(); }
        set
        {
            userAccount = value;
            ViewState["userAccount"] = value;

        }
    }
    //账户金额
    private string accountMoney;
    public string AccountMoney
    {
        get
        { return accountMoney; }
        set
        {
            accountMoney = value;

        }
    }

    protected string LjScore;
    protected string SpecialScore;

    #endregion

    protected override void OnInit(EventArgs e)
    {
        if (Session["UserID"] == null)
        {
            Response.Redirect("~/MemberLogin.aspx");
        }

        base.OnInit(e);
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GetAccountInfo();
            GetUserStockSummary();
            GetUserAccountInfo();
        }
    }
    /// <summary>
    /// 初始化账户的金额信息(左侧列表)
    /// </summary>
    protected void GetUserAccountInfo()
    {
        SQLExpressDAL.TransferRecordsTableAdapters.UsersAccountTableAdapter adp = new SQLExpressDAL.TransferRecordsTableAdapters.UsersAccountTableAdapter();
        SQLExpressDAL.TransferRecords.UsersAccountDataTable tab = adp.GetSingleAccount(Session["UserID"].ToString());

        SQLExpressDAL.TransferRecords.UsersAccountRow dr = (SQLExpressDAL.TransferRecords.UsersAccountRow)tab.Rows[0];

        this.lblAB1.Text = dr.AB1.ToString("f2");
        this.lblAB2.Text = dr.AB2.ToString("f2");
        this.lblAB3.Text = dr.AB3.ToString("f2");
        this.lblAB4.Text = dr.AB4.ToString("f2");
    }

    public void GetAccountInfo()
    {
        SQLExpressDAL.UsersTableAdapters.UsersTableAdapter userAdp = new SQLExpressDAL.UsersTableAdapters.UsersTableAdapter();
        SQLExpressDAL.Users.UsersDataTable tab = userAdp.GetSingleUserInfo(Session["UserID"].ToString());

        DataRow dr = tab.Rows[0];

        UserAccount = dr["UserID"].ToString();
        UserName = dr["UserName"].ToString();

        SQLExpressDAL.DealRecord obj = new SQLExpressDAL.DealRecord();
        Hashtable htab = obj.GetMemAccountMoney(Session["UserID"].ToString());

        if (htab["IsSuccess"].ToString().Equals("1"))
        {
            AccountMoney = htab["Count"].ToString();
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('金额统计错误');", true);
            return;
        }

        //this.lblAccountMoney.Text = AccountMoney;
        this.lblAccount.Text = UserAccount;
        this.lblUserName.Text = UserName;

        this.lblLjScore.Text = dr["ljscore"].ToString();
        this.lblSpecialScore.Text = dr["specialscore"].ToString();

    }

    protected void lbtnLoginOut_Click(object sender, EventArgs e)
    {
        Session.Abandon();
        Response.Redirect("~/Default.aspx");
    }

    //获取股权摘要信息
    protected void GetUserStockSummary()
    {
        string msg = "";
        try
        {
            SQLExpressDAL.DealRecord obj = new SQLExpressDAL.DealRecord();
            DataTable tab = obj.GetUserStockSummary(Session["UserID"].ToString());


            if (tab.Rows.Count > 0)
            {
                int isSuccess = Convert.ToInt32(tab.Rows[0]["Success"]);

                if (isSuccess == 0)
                {
                    DataRow dr = tab.Rows[0];

                    Int32 closeCount = Convert.ToInt32(dr["closeCount"]);
                    Int32 circulateCount = Convert.ToInt32(dr["circulateCount"]);
                    Int32 salingCount = Convert.ToInt32(dr["salingCount"]);
                    Double price = Convert.ToDouble(dr["price"]);

                    this.lblLiuTongCount.Text = circulateCount.ToString();
                    this.lblCloseCount.Text = closeCount.ToString();
                    this.lblSalingCount.Text = salingCount.ToString();
                    this.lblTotalCount.Text = (closeCount + circulateCount + salingCount).ToString();
                    this.lblTotalMoney.Text = (closeCount * price + circulateCount * price + salingCount * price).ToString();
                }
                else
                {
                    msg = "股权摘要查询出错!";
                }

            }
            else
            {
                msg = "股权摘要查询出错!";
            }
        }
        catch (Exception ex)
        {
            msg = ex.Message.ToString();
        }
        finally
        {
            if (!string.IsNullOrEmpty(msg))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('" + msg + "');", true);
            }
        }

    }

    /// <summary>
    /// 获取会员的等级信息及投资总额
    /// </summary>
    protected string GetUserDengJiInfo()
    {
        string userInfo = "";

        try
        {
            SQLExpressDAL.DealRecord obj = new SQLExpressDAL.DealRecord();
            DataTable tab = obj.GetMemDengJiInfo(Session["UserID"].ToString());

            DataRow dr = null;

            if (tab.Rows.Count > 0)
            {
                dr = tab.Rows[0];
                userInfo = string.Format("投资额:{0}<br/>等级:{1}", Convert.ToDecimal(dr[0]).ToString("F2"), string.IsNullOrEmpty(dr[1].ToString()) ? "暂无" : dr[1]);
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('获取会员等级信息错误');", true);
        }

        return userInfo;

    }


    DataTable dt = null;
    DataView dv = null;

    protected double GetJiFen()
    {
         
        string parentID = Session["UserID"].ToString();


        SQLExpressDAL.UsersTableAdapters.UsersTableAdapter userAdp = new SQLExpressDAL.UsersTableAdapters.UsersTableAdapter();
        SQLExpressDAL.Users.UsersDataTable userTable = userAdp.GetAllUsers();
        dt = (DataTable)userTable;

        dv = new DataView(dt);
        //筛选(相当于select * from tabMenu where menuParentID = 传入的 parentID) 
        dv.RowFilter = " [cdefine1]='" + parentID + "'";

        bool flag = ComputeSpecial(parentID, dt);
        double sum = 0;
        if (flag)
        {

            foreach (DataRowView row in dv)
            {
                string sScore = row["SpecialScore"].ToString();
                sum += string.IsNullOrEmpty(sScore) ? 0 : Convert.ToDouble(sScore);
            }

        }

        return  sum;
    }

    public bool ComputeSpecial(String parentID, DataTable dt)
    {
        bool flag = false;

        //实例化一个DataView dt = 传入的DataTable 
        dv = new DataView(dt);
        //筛选(相当于select * from tabMenu where menuParentID = 传入的 parentID) 
        dv.RowFilter = " [cdefine1]='" + parentID + "'";

        foreach (DataRowView row in dv)
        {
            string zhituiren = row["parentID"].ToString();

            if (zhituiren.Equals(parentID))
            {
                flag = true;
                break;
            }
            ComputeSpecial(row["userid"].ToString(), dt);

        }

        return flag;

    }
}
